<template>
  <div class="login-container">
    <el-card class="login-card">
      <template #header>
        <h2>系统登录</h2>
      </template>

      <el-form
        ref="loginForm"
        :model="loginData"
        :rules="rules"
        label-width="0"
        size="large"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginData.username"
            placeholder="用户名"
            prefix-icon="User"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginData.password"
            type="password"
            placeholder="密码"
            prefix-icon="Lock"
            show-password
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            class="login-button"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>

        <div class="login-options">
          <el-checkbox v-model="loginData.remember">记住我</el-checkbox>
          <el-link type="primary" @click="$router.push('/register')">
            注册账号
          </el-link>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { useRouter } from "vue-router";
import { useUserStore } from "@/store/user";
import { ElMessage } from "element-plus";
import { User, Lock } from "@element-plus/icons-vue";

const router = useRouter();
const userStore = useUserStore();
const loginForm = ref(null);
const loading = ref(false);

const loginData = reactive({
  username: "",
  password: "",
  remember: false,
});

const rules = {
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 3, max: 20, message: "长度在 3 到 20 个字符", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" },
  ],
};

const handleLogin = async () => {
  if (!loginForm.value) return;

  try {
    await loginForm.value.validate();
    loading.value = true;
    const res = await userStore.loginAction(
      loginData.username,
      loginData.password
    );

    if (res.status === 201) {
      ElMessage.success("登录成功");
      router.push("/admin");
    }
  } catch (error) {
    console.error("登录失败:", error);
    ElMessage.error(error.message || "登录失败");
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped lang="scss">
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url(@/assets/login.jpeg);

  .login-card {
    width: 400px;

    :deep(.el-card__header) {
      text-align: center;

      h2 {
        margin: 0;
        font-size: 24px;
        color: var(--text-color);
      }
    }

    .login-button {
      width: 100%;
    }

    .login-options {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-top: 20px;
    }
  }
}
</style>
